{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Warp PyTorch Tutorial: Basics\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install warp-lang torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warp as wp\n",
    "import numpy as np\n",
    "import torch\n",
    "\n",
    "wp.config.quiet = True\n",
    "\n",
    "# Explicitly initializing Warp is not necessary but\n",
    "# we do it here to ensure everything is good to go.\n",
    "wp.init()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Converting Arrays To/From PyTorch\n",
    "\n",
    "Warp provides helper functions to convert arrays to/from PyTorch without copying the underlying data (regardless of device). If an associated gradient array exists, this will be converted simultaneously."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Warp -> PyTorch\"\"\"\n",
    "\n",
    "# Construct a Warp array, including gradient array\n",
    "w = wp.array([1.0, 2.0, 3.0], dtype=wp.float32, requires_grad=True, device=wp.get_device())\n",
    "\n",
    "# Convert to Torch tensor\n",
    "t = wp.to_torch(w)\n",
    "\n",
    "print(t)\n",
    "print(t.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"PyTorch -> Warp\"\"\"\n",
    "\n",
    "# Construct a Torch tensor, including gradient tensor\n",
    "t = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32, requires_grad=True, device=torch.device(\"cuda:0\"))\n",
    "\n",
    "# Convert to Warp array\n",
    "w = wp.from_torch(t)\n",
    "\n",
    "print(w)\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"PyTorch -> Warp (+ allocate a new gradient array)\"\"\"\n",
    "\n",
    "# Construct a Torch tensor, excluding gradient tensor\n",
    "t = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32, requires_grad=False, device=torch.device(\"cuda:0\"))\n",
    "\n",
    "# Convert to Warp array and allocate a gradient array\n",
    "w = wp.from_torch(t, requires_grad=True)\n",
    "\n",
    "print(w)\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Map Warp device and dtype to Torch device and dtype\"\"\"\n",
    "\n",
    "# Warp device + dtype to torch\n",
    "device = wp.device_to_torch(wp.get_device())\n",
    "dtype = wp.dtype_to_torch(wp.float32)\n",
    "\n",
    "# Construct a Torch tensor, ensuring we are using the same dtype/device as Warp\n",
    "t = torch.tensor([1.0, 2.0, 3.0], dtype=dtype, device=device)\n",
    "\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Map Torch device and dtype to Warp device and dtype\"\"\"\n",
    "\n",
    "# Torch device + dtype from torch\n",
    "device = wp.device_from_torch(torch.device(\"cuda:0\"))\n",
    "dtype = wp.dtype_from_torch(torch.float32)\n",
    "\n",
    "# Construct a Warp array, ensuring we are using the same dtype/device as Torch\n",
    "w = wp.array([1.0, 2.0, 3.0], dtype=dtype, device=device)\n",
    "\n",
    "print(w)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
